package com.util.boxs.paints;

import com.util.boxs.bodys.BaseBody;
import com.util.boxs.bodys.CircleBody;
import com.util.boxs.bodys.HitBody;
import com.util.boxs.bodys.RectBody;

import java.awt.*;

public abstract class PaintableObject implements Paintable {
    protected BaseBody body;
    private boolean visible = true;
    /**
     * 创建一个圆形可绘图物
     * @param pixelX      X坐标
     * @param pixelY      Y坐标
     * @param pixelRadius 半径
     */
    public PaintableObject(int pixelX, int pixelY, int pixelRadius){
        body = new CircleBody(pixelX,pixelY,pixelRadius);
    }

    /**
     * 创建一个矩形可绘图物
     * @param pixelX
     * @param pixelY
     * @param width
     * @param height
     */
    public PaintableObject(int pixelX, int pixelY, int width, int height){
        body = new RectBody(pixelX,pixelY,width,height);
    }

    @Override
    public void paintImage(Graphics2D g) {
        if(visible) {
            float centerX = body.getPixelCenterX();
            float centerY = body.getPixelCenterY();
            double radians = -body.getBody().getAngle();//弧度
            g.rotate(radians, centerX, centerY);
            this.getImage().paintIcon(null, g, body.getPixelX(), body.getPixelY());
            g.rotate(-radians, centerX, centerY);
        }
    }

    public void setVisible(boolean visible){
        this.visible = visible;
    }

    public BaseBody getBody(){
        return body;
    }

    public HitBody getHitBody(){
        return body instanceof HitBody?(HitBody)body:null;
    }
}
